package servlet;

import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UpdateUserServlet", value = "/UpdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String userId=request.getParameter("id");
        String userName=request.getParameter("name");
        String birth=request.getParameter("birth");
        String userPassword=request.getParameter("pwd");
        String rePassword=request.getParameter("repwd");
        String sex=request.getParameter("sex");
        String address=request.getParameter("address");
        String type=request.getParameter("type");
        String regdate=request.getParameter("regdate");
        UserService userService=new UserServiceImpl();
        User user=new User(userName,userId,birth,address,sex,userPassword,type,regdate);
        if(rePassword.equals(userPassword)){
            if(userService.updateUserById(userId,user)){
                response.getWriter().print("更新成功！1s后返回！");
                response.setHeader("refresh","1;url=QueryUsersByPageServlet");
            }else{
                response.getWriter().print("更新失败！1s后返回！");
                response.setHeader("refresh","1;url=QueryUsersByPageServlet");
            }
        }else{
            response.getWriter().print("密码不一致！更新失败！1s后返回！");
            response.setHeader("refresh","1;url=QueryUsersByPageServlet");
        }
    }
}
